package rs.mbs2.epics.server.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@SuppressWarnings("serial")
@Entity
@Table(name="comment")
@NamedQueries({
	@NamedQuery(name="findPhotoComments", 
				query="SELECT c FROM Comment c WHERE c.commentedPhoto.id " +
					"LIKE :photoId"),
	
	@NamedQuery(name="findUserComments", 
					query="SELECT c FROM Comment c WHERE "
							+ "c.user.id LIKE :userId")
})
public class Comment implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "comment_id", unique = true, nullable = false)
	private Integer id;
	
	@Column(name = "comment_title", unique = false, nullable = false)
	private String  title;
	
	@Column(name = "comment_contents", unique = false, nullable = false)
	private String  contents;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "comment_date", unique = false, nullable = false)
	private Date    date;
	
	@ManyToOne
	@JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false)
	private User    user;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "photo_id", referencedColumnName = "photo_id", nullable = false)
	private Photo   photo;
	
	
	public Comment() {
		
	}
	
	public Comment(String title, String contents, Date date, User user,
			Photo photo) {
		
		super();
		this.title = title;
		this.contents = contents;
		this.date = date;
		this.user = user;
		this.photo = photo;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Comment other = (Comment) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
